<?php
/***************************************************************
* Subs-SMFModCreator.php                                       *
*--------------------------------------------------------------*
* SMF Mod Creator                                              *
* Version 0.1 Alpha                                            *
* Written By: Labradoodle-360                                  *
*--------------------------------------------------------------*
* Copyright 2011 Matthew Kerle - All Rights Reserved           *
*--------------------------------------------------------------*
* Redistribution and use in source and binary forms, with or   *
* without modification, are permitted provided that the        *
* conditions specified in the attached                         *
* smfmodcreator_license.txt file are met.                      *
***************************************************************/
if (!defined('SMF'))
	die ('Hacking Attempt...');

/* This function generates the list data */
function listModifications($start, $items_per_page, $sort)
{

	// Globalize everything we'll need...
	global $smcFunc, $txt, $scripturl, $settings, $context, $sourcedir, $memberContext;

	// Query Query Query - Lucy Lucy Lucy!
	$request = $smcFunc['db_query']('', '
		SELECT modi.id_modification, modi.mod_name, modi.id_category,
				modi.author_id, modi.submitted, modi.modified,
				modi.modified_id, modi.identifier, cate.id_category, cate.category_name
		FROM {db_prefix}modifications AS modi
		INNER JOIN {db_prefix}modification_categories AS cate ON (modi.id_category = cate.id_category)
		ORDER BY ' . $sort . '
		LIMIT ' . $start . ', ' . $items_per_page . ''
	);

	// Our Array
	$mods_list = array();

	// Query Results...
	while ($data = $smcFunc['db_fetch_assoc']($request))
	{
		// Author Name & ID
		if (!isset($memberContext[$data['author_id']]))
		{
			loadMemberData(array($data['author_id']), false, 'normal');
			loadMemberContext($data['author_id']);
		}

		// Modified Name, ID, & Time
		$data['modified'] = empty($data['modified']) || $data['submitted'] == $data['modified'] ? $txt['smfmc_never'] : timeformat($data['modified']);
		if (!empty($data['modified_id']) && $data['modified_id'] != $data['author_id'] && $data['submitted'] != $data['modified'] && !isset($memberContext[$data['modified_id']]))
		{
			loadMemberData(array($data['modified_id']), false, 'normal');
			loadMemberContext($data['modified_id']);
			$modified_colour = !empty($memberContext[$data['modified_id']]['group_color']) ? ' style="color: ' . $memberContext[$data['modified_id']]['group_color'] . ';"' : '';
			$data['modified'] .= ' <strong>' . $txt['by'] . '</strong> <a href="' . $scripturl . '?action=profile;u=' . $data['modified_id'] . '" target="_self" title="' . $memberContext[$data['modified_id']]['name'] . '"' . $modified_colour . '>' . $memberContext[$data['modified_id']]['name'] . '</a>';
		}

		// Some logic - I love logic!
		$logic['group_color'] = !empty($memberContext[$data['author_id']]['group_color']) ? ' style="color: ' . $memberContext[$data['author_id']]['group_color'] . ';"' : '';

		// Create our array...
		$mods_list[$data['id_modification']] = array(
			'name' => '<a href="' . $scripturl . '?action=modcreator;view_mod=' . $data['identifier'] . '" target="_self" title="View ' . $data['mod_name'] . '">' . $data['mod_name'] . '</a>',
			'category' => $data['category_name'],
			'author' => '<a href="' . $scripturl . '?action=profile;u=' . $data['author_id'] . '" target="_self" title="' . $memberContext[$data['author_id']]['name'] . '"' . $logic['group_color'] . '>' . $memberContext[$data['author_id']]['name'] . '</a>',
			'submitted' => timeformat($data['submitted']),
			'modified' => $data['modified'],
			'actions' => '<a href="' . $scripturl . '?action=modcreator;view_mod=' . $data['identifier'] . '" target="_self" title="' . $txt['smfmc_viewmod'] . '" style="text-decoration: none;">
						<img src="' . $settings['default_images_url'] . '/smfmodcreator/arrow.png" alt="" style="display: inline; padding: 2px;" class="rounded selector" />
					</a>
					<a href="' . $scripturl . '?action=modcreator;delete_mod=' . $data['id_modification'] . ';' . $context['session_var'] . '=' . $context['session_id'] . '" title="Delete Modification" target="_self" style="text-decoration: none;" onclick="return confirm(\'' . $txt['smfmc_yousurebro_del'] . $data['mod_name'] . '?\');">
						<img src="'. $settings['default_images_url']. '/smfmodcreator/cross.png" alt="" style="display: inline; padding: 2px;" class="rounded selector" />
					</a>',
		);
	}
	$smcFunc['db_free_result']($request);
	return $mods_list;
}

/* This function generates the count of modifications for the list */
function modification_count()
{
	global $smcFunc;
	$request = $smcFunc['db_query']('', '
		SELECT COUNT(id_modification) AS modification_count
		FROM {db_prefix}modifications'
	);
	list ($count) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);
	return $count;
}

/* This function simply generates an id->name array for validation */
function retrieve_modifications()
{

	// Globalize everything we'll need...
	global $modifications, $smcFunc;

	// Our Request
	$request = $smcFunc['db_query']('' , '
		SELECT modi.id_modification, modi.identifier
		FROM {db_prefix}modifications AS modi'
	);

	// On your marks, get set, go!
	$modifications = array();
	while ($data = $smcFunc['db_fetch_assoc']($request))
	{
		$modifications[$data['id_modification']] = $data['identifier'];
	}

	// This is just refreshing :)
	$smcFunc['db_free_result']($request);

	// Back to the surface!
	return $modifications;

}

/* This function retrieves the mod categories */
function retrieveCategories()
{

	// Globalize everything we'll need...
	global $categories, $smcFunc;

	// Our Request
	$request = $smcFunc['db_query']('', '
		SELECT cat.id_category, cat.category_name
		FROM {db_prefix}modification_categories AS cat'
	);

	// Our Array
	$categories = array();

	// Feed the Array
	while ($data = $smcFunc['db_fetch_assoc']($request))
	{
		$categories[$data['id_category']] = $data['category_name'];
	}

	// Free the results!
	$smcFunc['db_free_result']($request);

	// Return our array
	return $categories;

}

/* This function retrieves everything associated with each mod */
function singleModification($modID = '')
{

	// Globalize everything we'll need...
	global $modification, $scripturl, $smcFunc, $boardurl, $memberContext, $txt, $boarddir;

	// No messing around here folks.
	if (empty($modID))
		return;

	// Proceed with our request...
	$request = $smcFunc['db_query']('', '
		SELECT modi.id_modification, modi.mod_name,
			modi.id_category, modi.summary, modi.author_id,
			modi.submitted, modi.modified, modi.modified_id,
			modi.identifier, cate.id_category, cate.category_name
		FROM {db_prefix}modifications AS modi
		LEFT JOIN {db_prefix}modification_categories AS cate ON (modi.id_category = cate.id_category)
		WHERE modi.id_modification = {int:modID}',
		array(
			'modID' => (int) $modID,
		)
	);

	// Our $modification array!
	$modification = array();
	while ($data = $smcFunc['db_fetch_assoc']($request))
	{

		if (!isset($memberContext[$data['author_id']]))
		{
			loadMemberData(array($data['author_id']), false, 'normal');
			loadMemberContext($data['author_id']);
		}
		$data['modified'] = empty($data['modified']) || $data['submitted'] == $data['modified'] ? 'Never' : timeformat($data['modified']);
		if (!empty($data['modified_id']) && $data['modified_id'] != $data['author_id'] && $data['submitted'] != $data['modified'] && !isset($memberContext[$data['modified_id']]))
		{
			loadMemberData(array($data['modified_id']), false, 'normal');
			loadMemberContext($data['modified_id']);
			$modified_colour = !empty($memberContext[$data['modified_id']]['group_color']) ? ' style="color: ' . $memberContext[$data['modified_id']]['group_color'] . ';"' : '';
			$data['modified'] .= ' <strong>' . $txt['by'] . '</strong> <a href="' . $scripturl . '?action=profile;u=' . $data['modified_id'] . '" target="_self" title="' . $memberContext[$data['modified_id']]['name'] . '"' . $modified_colour . '>' . $memberContext[$data['modified_id']]['name'] . '</a>';
			$modified = true;
		}
		$author_colour = !empty($memberContext[$data['author_id']]['group_color']) ? ' style="color: ' . $memberContext[$data['author_id']]['group_color'] . ';"' : '';
		$modification = array(
			'name' => $data['mod_name'],
			'identifier' => $data['identifier'],
			'category' => array(
				'id' => $data['id_category'],
				'name' => $data['category_name'],
			),
			'submitted' => timeformat($data['submitted']),
			'summary' => !empty($data['summary']) ? $data['summary'] : $txt['smfmc_nosummyet'],
			'author' => array(
				'id' => $data['author_id'],
				'name' => $memberContext[$data['author_id']]['name'],
				'href' => $scripturl . '?action=profile;u=' . $data['author_id'],
				'link' => '<a href="' . $scripturl . '?action=profile;u=' . $data['author_id'] . '" target="_self"' . $author_colour . '>' . $memberContext[$data['author_id']]['name'] . '</a>',
				'avatar' => $memberContext[$data['author_id']]['avatar']['image'],
				'group' => $memberContext[$data['author_id']]['group'],
				'online' => $memberContext[$data['author_id']]['online']['is_online'],
			),
			'modified_time' => $data['modified'],
			'modified_files' => array(
			),
		);
		// Modified Data?
		if (isset($modified))
		{
			$modification['modified'] = array(
				'user' => array(
					'id' => $data['modified_id'],
					'name' => $memberContext[$data['modified_id']]['name'],
					'href' => $scripturl . '?action=profile;u=' . $data['modified_id'],
					'link' => '<a href="' . $scripturl . '?action=profile;u=' . $data['modified_id'] . '" target="_self"' . $modified_colour . '>' . $memberContext[$data['modified_id']]['name'] . '</a>',
					'avatar' => $memberContext[$data['modified_id']]['avatar']['image'],
				),
			);
		}
		// Any modifications?
		if (file_exists($boarddir . '/smfmodcreator/modifications/' . $data['identifier'] . '/modifications.xml'))
		{
			$xml_data = file_get_contents($boardurl . '/smfmodcreator/modifications/' . $data['identifier'] . '/modifications.xml');
			$xml_data = uncdata($xml_data);
			$simplexml_data = simplexml_load_string($xml_data);
			foreach ($simplexml_data->file as $contents)
			{
				echo '<pre>', print_r($contents->file->name), '</pre>';
			}
		}
	}

	// Weee!
	$smcFunc['db_free_result']($request);

	// Return
	return $modification;

}

?>